package pjkanri.controller;

import java.io.IOException;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import pjkanri.dao.DAOException;
import pjkanri.entity.Issure;
import pjkanri.service.IssueService;

/**
 * Servlet implementation class IssureServlet
 */
@WebServlet("/createissure")
public class CreateIssureServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String description = request.getParameter("description");
		String name = request.getParameter("name");
		int type_id = 0;
		int account_id = 0;
		Integer status = 0;

		// セッション管理開始
		HttpSession session = request.getSession();
		System.out.println("Issu" + session.getAttribute("projectId"));
		
		// セッション・オブジェクトにAccountオブジェクトをセット
		Integer projectId = Integer.parseInt(session.getAttribute("projectId")
				.toString());

		Date date = new Date();

		Issure issure = new Issure(null, projectId, name, description, type_id,
				date, account_id, status);
		try {
			IssueService.insert(issure);
		} catch (DAOException e) {
			e.printStackTrace();
		}

		String forwardPath = "/issurelist" ;//"_issureList.jsp";
		request.setAttribute("projectId", projectId);

		// 転送処理
		request.getRequestDispatcher(forwardPath).forward(request, response);

//		ServletContext sc = getServletContext();  
//		RequestDispatcher rd = sc.getRequestDispatcher("/issurelist");  
//		rd.forward(request, response);
	}

}
